package com.jcl.recruit.api.wechat.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.web.utils.SecurityUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.basic.login.model.LoginResult;
import com.jcl.recruit.basic.login.model.UserExternalLoginInfo;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.wechat.service.WechatLoginService;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @ClassName WechatRegisterAndLoginController

 * @Author yyliu
 * @Date 2018/9/14 10:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/wechat")
public class WechatLoginController {

    private static final Logger logger = LoggerFactory.getLogger(WechatLoginController.class);

    @Autowired
    private WechatLoginService wechatLoginService;

    /* *
     * @Author yyliu
     * @Description 微信登陆
     * @Date 19:46 2018/9/12
     * @Param [code, state, request, response]
     * @return void
     **/
    @RequestMapping("/access")
    public ApiResponse access(String code, String state) throws IOException {

        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_NULL_MSG);
        }

        WechatUtils.AccessInfo accessInfo = new WechatUtils.AccessInfo();

        // 获取登录用户身份
        WechatUtils.WechatUserInfo userInfo = wechatLoginService.getWechatUserInfo(code);
        if (state.indexOf(WechatUtils.LOGIN_STATE) > -1) {
            UserExternalLoginInfo loginInfo = wechatLoginService.accessLogin(userInfo, state);
            LoginResult result = wechatLoginService.doLogin(loginInfo.getCorpCode(), loginInfo.getUserId(),
                    SecurityUtils.FROM_WECHAT);
            accessInfo.setUserToken(result.getToken());
            accessInfo.setUserStatus(result.getUserStatus());
            accessInfo.setState(WechatUtils.LOGIN_STATE);
            if (state.indexOf(WechatUtils.LOGIN_STATE_CRAWLER_DOWNLOAD) > -1) {
                accessInfo.setRouterState(WechatUtils.LOGIN_STATE_CRAWLER_DOWNLOAD);
            }

        } else if (state.indexOf(WechatUtils.LOGIN_STATE_INVITE) > -1) {

            String accessKey = state.split(WechatUtils.LOGIN_STATE_SPLIT)[1];
            WechatUtils.AccessLoginInfo loginInfo = wechatLoginService.accessLoginByInvite(userInfo, accessKey);
            accessInfo.setMsg("操作成功");
            if ("login".equals(loginInfo.getCode())) {
                User user = loginInfo.getUser();
                LoginResult result = null;
                if (Constants.UserInfo.USER_STATUS_UNACTIVE == user.getStatus()) {
                    result = wechatLoginService.doLogin(user.getCorpCode(), user.getId(), SecurityUtils.FROM_PERMISS);
                } else {
                    result = wechatLoginService.doLogin(user.getCorpCode(), user.getId(), SecurityUtils.FROM_WECHAT);
                }
                accessInfo.setUserStatus(result.getUserStatus());
                accessInfo.setUserToken(result.getToken());
            } else if ("register".equals(loginInfo.getCode())) {
                // 跳转页面 完善注册信息
                logger.info("register userjoin  !!!");
                ConsoleUserAccess userAccess = loginInfo.getUserExternalAccess();
                accessInfo.setAccessId(userAccess.getId().toString());
                accessInfo.setAccessKey(accessKey);
            } else {
                accessInfo.setMsg(loginInfo.getMsg());
            }

            accessInfo.setState(WechatUtils.LOGIN_STATE_INVITE);
            accessInfo.setCode(loginInfo.getCode());
        }

        return ApiResponse.success(accessInfo);
    }
}
